﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NetServer
{
    public class ErrorRecordFile
    {
        public static void WriteError(Exception e)
        {
            string str = "";
            string strDateInfo = "出现应用程序未处理的异常：" + DateTime.Now.ToString() + "\r\n";
            Exception error = e;
            if (error != null)
            {
                str = string.Format(strDateInfo + "异常类型：{0}\r\n异常消息：{1}\r\n异常信息：{2}\r\n",
                     error.GetType().Name, error.Message, error.StackTrace);
            }
            else
            {
                str = string.Format("应用程序线程错误:{0}", e);
            }
            writeLog(str);
            MessageBox.Show("错误详情请查看日志文件！", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        public static void WriteError(string e)
        {
            string strDateInfo = "出现应用程序未处理的异常：" + DateTime.Now.ToString() + "\r\n";
            writeLog(strDateInfo+e);
        
        }
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="str"></param>
        private static void writeLog(string str)
        {
            string Time = DateTime.Now.ToString("yyyyMMdd");
            using (StreamWriter sw = new StreamWriter(@"ErrLog\\" + Time + "ErrLog.txt", true))
            {
                sw.WriteLine(str);
                sw.WriteLine("\n---------------------------------------------------------\n");
                sw.Close();
            }
        }
    }
}
